package message;
/*
 *  msgType
 *  0 = calltaker  -> server
 *  1 = server     -> dispatcher
 *  2 = dispatcher -> server
 *  3 = server     -> resource
 *  4 = resource   -> server
 *
 */

import java.io.*;

public class Message
{
    public static void main(String[] args)
    {
        // test print encodeMsg
        Task myTask = new Task(2, 1, 1, 27032010, 2200, "Kent Ridge", "COM1 on Fire");
        String temp = encodeMsg(myTask, 0);
        System.out.println(temp);

        // test print decodeMsg
        Task newTask = decodeMsg("3|0|0|0|27032010|2300|Kent Ridge|COM2 on Fire|2|Police Team 1|Fire Team 1");
        System.out.print(newTask.getTaskID() + "|" + 
                           newTask.getUrgency() + "|" + 
                           newTask.getEmergencyLevel() + "|" + 
                           newTask.getDate() + "|" +
                           newTask.getTime() + "|" +
                           newTask.getLocation() + "|" +
                           newTask.getDescription());
        System.out.print("|" + newTask.getNumResource());
        for (int i = 0; i < newTask.getNumResource(); i++)
        {
            System.out.print("|" + newTask.getResource(i));
        }
        System.out.println();
    }
    
    public static String encodeMsg(Task myTask, int msgType)
    {
        String temp;

        temp = msgType + "|" + 
               myTask.getTaskID() + "|" + 
               myTask.getUrgency() + "|" + 
               myTask.getEmergencyLevel() + "|" + 
               myTask.getDate() + "|" +
               myTask.getTime() + "|" +
               myTask.getLocation() + "|" +
               myTask.getDescription();

        if (msgType > 1)
        {
            temp = temp + "|" + myTask.getNumResource();
            for (int i = 0; i < myTask.getNumResource(); i++)
            {
                temp = temp + "|" + myTask.getResource(i);
            }
        }

        return temp;
    }

    public static Task decodeMsg(String myMsg)
    {
        Task myTask = new Task();
        String[] myArray = myMsg.split("\\|");
        
        switch (Integer.parseInt(myArray[0]))
        {
            // calltaker -> server
            case 0:
            case 1:
            myTask.setTaskID(Integer.parseInt(myArray[1]));
            myTask.setUrgency(Integer.parseInt(myArray[2]));
            myTask.setEmergencyLevel(Integer.parseInt(myArray[3]));
            myTask.setDate(Integer.parseInt(myArray[4]));
            myTask.setTime(Integer.parseInt(myArray[5]));
            myTask.setLocation(myArray[6]);
            myTask.setDescription(myArray[7]);
            break;

            case 2:
            case 3:
            case 4:
            myTask.setTaskID(Integer.parseInt(myArray[1]));
            myTask.setUrgency(Integer.parseInt(myArray[2]));
            myTask.setEmergencyLevel(Integer.parseInt(myArray[3]));
            myTask.setDate(Integer.parseInt(myArray[4]));
            myTask.setTime(Integer.parseInt(myArray[5]));
            myTask.setLocation(myArray[6]);
            myTask.setDescription(myArray[7]);
            myTask.setNumResource(Integer.parseInt(myArray[8]));
            for (int i = 0; i < Integer.parseInt(myArray[8]); i++)
            {
                myTask.setResource(i, myArray[(9+i)]);
            }
            break;

            default:
            // error
            
        }

        return myTask;
    }
}
